package Week.Week317;

import java.util.*;

public class p2 {
    class Solution {
        public List<List<String>> mostPopularCreator(String[] creators, String[] ids, int[] views) {
            HashMap<String, Integer> scoreMap = new HashMap<>();
            for (int i = 0; i < creators.length; i++) {
                scoreMap.put(creators[i], scoreMap.getOrDefault(creators[i], 0) + views[i]);
            }
            int max = Collections.max(scoreMap.values());
            LinkedList<String> author = new LinkedList<>();
            for (Map.Entry<String, Integer> entry : scoreMap.entrySet()) {
                if (entry.getValue() == max){
                    author.add(entry.getKey());
                }
            }
            LinkedList<PriorityQueue<String>> list = new LinkedList<>();

            for (int i = 0; i < author.size(); i++) {
                PriorityQueue<String> pq = new PriorityQueue<>();
                for (int j = 0; j < creators.length; j++) {
                    if (author.get(i).equals(creators[i])){
                        pq.add(ids[i]);
                    }
                }
                list.add(new PriorityQueue<>(pq));
            }

            LinkedList<String> item = new LinkedList<>();
            LinkedList<List<String>> ans = new LinkedList<>();
            for (int i = 0; i < author.size(); i++) {
                item.add(author.get(i));
                item.add(list.get(i).poll());
                ans.add(new LinkedList<>(item));
                item.clear();
            }
            return ans;
        }
    }
}
